<template>
    <div ref="rightPanel" :class="{show:isPopups}" class="m-popups">
      <div class="m-popups-bg"></div>
      <div class="m-popups-box">
        <div class="f-title">
          <p>{{ title }}</p>
          <i class="el-icon-close" @click="closePopups"></i>
        </div>
        <div v-if="typeId == 1" class="f-content">
          <el-form :model="menuForm" :rules="menuRules" ref="menuForms" label-width="80px">
            <el-form-item label="父级权限">
              <el-cascader style="width:300px;" :options="parentMenu" v-model="selectedOptions" :props="{ checkStrictly: true, value: 'menuCode', label: 'menuName' }" disabled clearable></el-cascader>
            </el-form-item>
            <el-form-item label="名称">
              <el-input class="inputWidth" v-model="menuForm.menuName" placeholder="请输入权实体名称"></el-input>
            </el-form-item>
            <el-form-item label="路径">
              <el-input class="inputWidth" v-model="menuForm.menuUrl" placeholder="请输入权实体名称"></el-input>
            </el-form-item>
            <el-form-item label="图标">
              <el-input class="inputWidth" v-model="menuForm.menuIcon" placeholder="请填写图标KEY或链接"></el-input>
            </el-form-item>
            <el-form-item label="描述">
              <el-input class="inputWidth" type="textarea" v-model="menuForm.menuRemark" placeholder="请添加描述"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">确认</el-button>
              <el-button @click="closePopups">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="typeId == 2" class="f-content">
          <el-form :model="menuForm" :rules="menuRules" ref="menuForms" label-width="80px">
            <el-form-item label="功能名称">
              <el-input class="inputWidth" v-model="menuForm.buttonName" placeholder="请输入功能名称"></el-input>
            </el-form-item>
            <el-form-item label="数据列">
              <el-select class="inputWidth" v-model="menuForm.dataListCode" placeholder="请选择列表" @change="changeDataListCode">
                <el-option v-for="item in dataCodeList" :key="item.dataListId" :label="item.dataListName" :value="item.dataListCode"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="KEY">
              <el-select class="inputWidth" v-model="menuForm.key" placeholder="请选择KEY">
                <el-option v-for="item in keyList" :key="item.key" :label="item.desc" :value="item.key"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">确认</el-button>
              <el-button @click="closePopups">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="typeId == 3" class="f-content">
          <el-form :model="menuForm" :rules="menuRules" ref="menuForms" label-width="80px">
            <el-form-item label="列表名称">
              <el-input class="inputWidth" v-model="menuForm.dataListName" placeholder="请输入列表名称"></el-input>
            </el-form-item>
            <el-form-item label="选择列表">
              <el-select class="inputWidth" v-model="menuForm.dataInterfaceId" placeholder="请选择列表">
                <el-option v-for="item in columnList" :key="item.dataInterfaceId" :label="item.dataInterfaceName" :value="item.dataInterfaceId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">确认</el-button>
              <el-button @click="closePopups">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="typeId == 4" class="f-content">
          高级搜索
        </div>
        <div v-if="typeId == 5" class="f-content">
          <el-table ref="multipleTable" :data="tableData" border tooltip-effect="dark" style="width: 100%" :header-cell-style="{textAlign: 'center'}" :cell-style="{ textAlign: 'center' }">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="name" label="列名" min-width="100"></el-table-column>
            <el-table-column label="设置" min-width="100">

            </el-table-column>
          </el-table>
          <div class="m-btn">
            <el-button type="primary" @click="onSubmit">确认</el-button>
            <el-button @click="closePopups">取消</el-button>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
import { reactive, ref, toRefs, watch, computed } from 'vue'
import { menuTree, menuAdd, dataInterfacePage, buttonAdd, dataListAdd, dataListPage, getButtonByDataList } from '@/api/menu'
import { ElMessage } from 'element-plus'
import { addClass, removeClass } from '@/utils'

export default {
  name: 'MenuPopups',
  props: {
    isPopups: Boolean,
    typeId: Number,
    menuItems: String,
    menuCode: String,
    parentData: Object,
    menuIdArr: Array
  },
  setup(props, context) {
    const menuForms = ref(null)
    const state = reactive({
      menuForm: {
        menuType: '1'
      },
      tableData: [{
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
      }],
      menuRules: {
        parentMenuId: [{ required: true, message: '请选择父级权限', trigger: 'blur' }],
        menuName: [{ required: true, message: '请输入权实体名称', trigger: 'blur' }]
      },
      parentMenu: [],
      columnList: [],
      dataCodeList: [],
      keyList: [],
      selectedOptions: []
    })
    watch(() => props.isPopups,(newProps, oldProps) => {
        if (newProps) {
          addClass(document.body, 'showRightPanel')
        } else {
          removeClass(document.body, 'showRightPanel')
        }
        if (newProps && props.typeId == 1) {
          const data = {
            menuItems: props.menuItems
          }
          if (props.menuItems == 1) {
            data.systemCode = 0
          } else {
            data.systemCode = 1
          }
          menuTree(data).then((res) => {
            if (res.code === 200) {
              state.parentMenu = res.data
              state.selectedOptions = props.menuIdArr
            }
          })
        } else if (newProps && props.typeId == 2) {
          dataListPage({ code: props.parentData.menuCode, num: 1, size: 20, type: 0, state: 0 }).then((res) => {
            if (res.code === 200) {
              state.dataCodeList = res.data.data
            }
          })
        } else if (newProps && props.typeId == 3) {
          dataInterfacePage({size: 30}).then((res) => {
            if (res.code === 200) {
              state.columnList = res.data.data
            }
          })
        }
      }
    )
    const title = computed(() => {
      if (props.typeId == 1) {
        return '新增菜单'
      } else if (props.typeId == 2) {
        return '新增功能权限'
      } else if (props.typeId == 3) {
        return '新增列权限'
      } else if (props.typeId == 4) {
        return '高级搜索设定'
      } else if (props.typeId == 5) {
        return '设置快捷搜索'
      } else if (props.typeId == 6) {
        return '设置高级搜索'
      }
    })
    const closePopups = () => {
      const isPopups = false
      state.selectedOptions = []
      context.emit('closePop', isPopups)
    }
    const onSubmit = () => {
      menuForms.value.validate((valid) => {
        if (valid) {
          if (props.typeId == 1) {
            let data = {
              systemCode: parseInt(props.parentData.systemCode),
              menuItems: props.menuItems,
              menuName: state.menuForm.menuName,
              menuUrl: state.menuForm.menuUrl,
              menuIcon: state.menuForm.menuIcon,
              menuRemark: state.menuForm.menuRemark
            }
            if (state.selectedOptions.length > 0) {
              data.parentMenuCode = props.menuCode
            } else {
              data.parentMenuCode = '0'
            }
            if (state.selectedOptions.length >= 2) {
              data.menuType = '2'
            } else {
              data.menuType = '1'
            }
            menuAdd(data).then((res) => {
              if (res.code === 200) {
                ElMessage.success({
                  message: res.message,
                  type: 'success'
                })
                window.location.reload()
                context.emit('closePop', false)
              }
            }).catch((err) => {
              ElMessage.error(err)
            })
          } else if (props.typeId == 2) {
            let data = {
              buttonName: state.menuForm.buttonName,
              key: state.menuForm.key,
              dataListCode: state.menuForm.dataListCode,
              code: props.parentData.menuCode
            }
            buttonAdd(data).then((res) => {
              if (res.code === 200) {
                ElMessage.success({
                  message: res.message,
                  type: 'success'
                })
                window.location.reload()
                context.emit('closePop', false)
              }
            }).catch((err) => {
              ElMessage.error(err)
            })
          } else if (props.typeId == 3) {
            let data = {
              dataInterfaceId: state.menuForm.dataInterfaceId,
              dataListName: state.menuForm.dataListName,
              code: props.parentData.menuCode
            }
            dataListAdd(data).then((res) => {
              if (res.code === 200) {
                ElMessage.success({
                  message: res.message,
                  type: 'success'
                })
                window.location.reload()
                context.emit('closePop', false)
              }
            }).catch((err) => {
              ElMessage.error(err)
            })
          }
        } else {
          return false
        }
      })
    }
    const changeDataListCode = (data) => {
      if (data) {
        getButtonByDataList({code: data}).then((res) => {
          if (res.code === 200) {
            state.keyList = res.data
          } 
        }) 
      }
    }
    return {
      ...toRefs(state),
      title,
      menuForms,
      closePopups,
      onSubmit,
      changeDataListCode
    }
  }
}
</script>

<style>
.showRightPanel {
  overflow: hidden;
  position: relative;
  width: calc(100% - 15px);
}
</style>

<style lang="less" scoped>
.m-popups {
  .m-popups-bg {
    position: fixed;
    top: 0;
    left: 0;
    opacity: 0;
    transition: opacity .3s cubic-bezier(.7, .3, .1, 1);
    background: rgba(0, 0, 0, .6);
    z-index: -1;
  }
  .m-popups-box {
    width: 100%;
    max-width: 1000px;
    height: 100vh;
    position: fixed;
    top: 0;
    right: 0;
    box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, .05);
    transition: all .25s cubic-bezier(.7, .3, .1, 1);
    transform: translate(100%);
    background: #fff;
    z-index: 11;
    border-radius: 26px 0 0 26px;
    .f-title {
      display: flex;
      width: 90%;
      height: 60px;
      margin: 0 auto 30px;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #cccccc;
      p {
        font-size: 16px;
      }
      i {
        cursor: pointer;
        font-size: 16px;
      }
    }
    .f-content {
      width: 90%;
      height: 80%;
      margin: 0 auto 12px;
      padding: 12px;
      font-size: 14px;
      overflow: auto;
      overflow-x: hidden;
      box-sizing: border-box;
    }
    .f-btn {
      width: 90%;
      margin: 0 auto;
      text-align: right;
    }
  }
  .inputWidth {
    width: 300px;
  }
  .el-table td, .el-table th {
    text-align: center;
  }
  .m-btn {
    margin-top: 20px;
    text-align: center;
  }
}
.show {
  transition: all .6s cubic-bezier(.7, .3, .1, 1);

  .m-popups-bg {
    z-index: 10;
    opacity: 1;
    width: 100%;
    height: 100%;
  }

  .m-popups-box {
    transform: translate(0);
  }
}
</style>
